#ifndef __DUART_H__
#define __DUART_H__
/*
	MC68681 DUART driver function and macro declarations
	
	Part of the as-yet-unnamed MC68010 operating system


	(c) Stuart Wallace, December 2011.
*/

#include "include/types.h"

/*
	Base address of MC68681 DUART IC
*/
#define DUART_BASE		((u8 *) 0x00e00000)


/*
	DUART registers

	offset			read								write									
	--------------------------------------------------------------------------------------------
	0				Mode Register A (MR1A, MR2A)		Mode Register A (MR1A, MR2A)			 */
#define DUART_MRA		*((vu8 *) (DUART_BASE + 0x00))

/*	1				Status Register A (SRA)				Clock Select Register A (CSRA)			 */
#define DUART_SRA		*((vu8 *) (DUART_BASE + 0x02))
#define DUART_CSRA		*((vu8 *) (DUART_BASE + 0x02))

/*	2				BRG test							Command register A (CRA))				 */
#define DUART_BRG_TEST	*((vu8 *) (DUART_BASE + 0x04))
#define DUART_CRA		*((vu8 *) (DUART_BASE + 0x04))

/*	3				Rx Holding Register A (RHRA)		Tx Holding Register A (THRA))			 */
#define DUART_RHRA		*((vu8 *) (DUART_BASE + 0x06))
#define DUART_THRA		*((vu8 *) (DUART_BASE + 0x06))

/*	4				Input Port Change Register (IPCR)	Aux Control Register (ACR))				 */
#define DUART_IPCR		*((vu8 *) (DUART_BASE + 0x08))
#define DUART_ACR		*((vu8 *) (DUART_BASE + 0x08))

/*	5				Interrupt Status Register (ISR)		Interrupt Mask Register (IMR))			 */
#define DUART_ISR		*((vu8 *) (DUART_BASE + 0x0a))
#define DUART_IMR		*((vu8 *) (DUART_BASE + 0x0a))

/*	6				Counter/Timer Upper Value (CTU)		Counter/Timer Upper Preset Value (CTUR)) */
#define DUART_CTU		*((vu8 *) (DUART_BASE + 0x0c))
#define DUART_CTUR		*((vu8 *) (DUART_BASE + 0x0c))

/*	7				Counter/Timer Lower Value (CTL)		Counter/Timer Lower Preset Value (CTLR)) */
#define DUART_CTL		*((vu8 *) (DUART_BASE + 0x0e))
#define DUART_CTLR		*((vu8 *) (DUART_BASE + 0x0e))

/*	8				Mode Register B (MR1B, MR2B)		Mode Register B (MR1B, MR2B))			 */
#define DUART_MRB		*((vu8 *) (DUART_BASE + 0x10))

/*	9				Status Register B (SRB)				Clock Select Register B (CSRB))			 */
#define DUART_SRB		*((vu8 *) (DUART_BASE + 0x12))
#define DUART_CSRB		*((vu8 *) (DUART_BASE + 0x12))

/*	10				1x/16x Test							Command Register B (CRB))				 */
#define DUART_1_16_TEST	*((vu8 *) (DUART_BASE + 0x14))
#define DUART_CRB		*((vu8 *) (DUART_BASE + 0x14))

/*	11				Rx Holding Register B (RHRB)		Tx Holding Register B (THRB))			 */
#define DUART_RHRB		*((vu8 *) (DUART_BASE + 0x16))
#define DUART_THRB		*((vu8 *) (DUART_BASE + 0x16))

/*	12				Interrupt Vector Register (IVR)		Interrupt Vector Register (IVR))		 */
#define DUART_IVR		*((vu8 *) (DUART_BASE + 0x18))

/*	13				Input Ports IP0-IP6					Output Port Conf Register (OPCR))		 */
#define DUART_IP		*((vu8 *) (DUART_BASE + 0x1a))
#define DUART_OPCR		*((vu8 *) (DUART_BASE + 0x1a))

/*	14				Start Counter Command (START_CC)	Set Output Port Bits Command (SOPR))	 */
#define DUART_START_CC	*((vu8 *) (DUART_BASE + 0x1c))
#define DUART_SOPR		*((vu8 *) (DUART_BASE + 0x1c))

/*	15				Stop Counter Command (STOP_CC)		Reset Output Port Bits Command (ROPR))	 */
#define DUART_STOP_CC	*((vu8 *) (DUART_BASE + 0x1e))
#define DUART_ROPR		*((vu8 *) (DUART_BASE + 0x1e))


/*
	DUART register bits
*/

/* DUART_SR[AB] */
#define DUART_SR_RECEIVED_BREAK		(7)
#define DUART_SR_FRAMING_ERROR		(6)
#define DUART_SR_PARITY_ERROR		(5)
#define DUART_SR_OVERRUN_ERROR		(4)
#define DUART_SR_TXEMT				(3)
#define DUART_SR_TXRDY				(2)
#define DUART_SR_FFULL				(1)
#define DUART_SR_RXRDY				(0)


/*
	LED port bits
*/
#define LED_RED						(0x80)
#define LED_GREEN					(0x40)


void duart_init(void);
inline int duarta_putc(const char c);
inline int duarta_getc(void);

void led_on(const unsigned char leds);
void led_off(const unsigned char leds);

#endif

